home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
Other Langs
/
Tickle-4.0 (tcl)
/
src
/
tcl-dbm.c
< prev
next >
Wrap
Text File
|
1993-11-19
|
10KB
|
479 lines
/*
** This source code was written by Tim Endres
** Email: time@ice.com.
** USMail: 8840 Main Street, Whitmore Lake, MI 48189
**
** Some portions of this application utilize sources
** that are copyrighted by ICE Engineering, Inc., and
** ICE Engineering retains all rights to those sources.
**
** Neither ICE Engineering, Inc., nor Tim Endres,
** warrants this source code for any reason, and neither
** party assumes any responsbility for the use of these
** sources, libraries, or applications. The user of these
** sources and binaries assumes all responsbilities for
** any resulting consequences.
*/
#pragma segment TCLDBM
#include "tickle.h"
#include "tcl.h"
#include "sdbm.h"
extern int errno;
extern int macintoshErr;
typedef struct {
DBM *dbm;
char name[32];
} DBM_NAMED_DB;
#define MAX_DBMS 8
DBM_NAMED_DB _dbms_[MAX_DBMS];
init_tcl_dbm()
{
int i;
for (i=0; i<MAX_DBMS; ++i)
{
_dbms_[i].dbm = (DBM *)0;
_dbms_[i].name[0] = '\0';
}
}
close_tcl_dbm()
{
int i;
for (i=0; i<MAX_DBMS; ++i)
{
if (_dbms_[i].dbm != (DBM *)0)
dbm_close(_dbms_[i].dbm);
}
}
int
Cmd_DBMOpen(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
short wdRefNum;
int index, push_err, myerr;
#pragma unused (clientData)
if (argc != 4)
{
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" DBName idxFileName datFileName\"", (char *) NULL);
return TCL_ERROR;
}
for (index = 0 ; index < MAX_DBMS ; ++index)
{
if (_dbms_[index].dbm == NULL)
break;
if (strcmp(_dbms_[index].name, argv[1]) == SAMESTR)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" duplicate DB name '",
argv[1], "'", (char *) NULL);
return TCL_ERROR;
}
}
if (index >= MAX_DBMS)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" max DB's open", (char *) NULL);
return TCL_ERROR;
}
else
{
myerr = TclMac_CWDCreateWD(&wdRefNum);
if (myerr != noErr)
{
Tcl_AppendResult(interp, "could not create working directory - ",
Tcl_MacGetError(interp, myerr), NULL);
return TCL_ERROR;
}
push_err = TclMac_CWDPushVol();
SetVol(NULL, wdRefNum);
_dbms_[index].dbm = dbm_x_open(argv[2], argv[3], O_RDWR | O_CREAT, 0666);
if (push_err == noErr)
TclMac_CWDPopVol();
TclMac_CWDDisposeWD(wdRefNum);
if (_dbms_[index].dbm == (DBM *)0)
{
strcpy(_dbms_[index].name, "--CLOSED--");
Tcl_AppendResult(interp, "\"", argv[0], "\" error opening DB", (char *) NULL);
return TCL_ERROR;
}
else
{
strcpy(_dbms_[index].name, argv[1]);
return TCL_OK;
}
}
}
int
Cmd_DBMInsert(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
int index;
datum key, data;
#pragma unused (clientData)
if (argc != 4 && argc != 5)
{
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" DBName key data ?replace?\"", (char *) NULL);
return TCL_ERROR;
}
for (index = 0 ; index < MAX_DBMS ; ++index)
{
if (strcmp(_dbms_[index].name, argv[1]) == SAMESTR)
break;
}
if (index >= MAX_DBMS)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" DB \"",
argv[1], "\" not found", (char *) NULL);
return TCL_ERROR;
}
else
{
key.dptr = argv[2];
key.dsize = strlen(argv[2]);
data.dptr = argv[3];
data.dsize = strlen(argv[3]);
if (dbm_store(_dbms_[index].dbm, key, data, (argc == 4 ? DBM_INSERT : DBM_REPLACE)) < 0)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" error storing data", (char *) NULL);
return TCL_ERROR;
}
else
{
return TCL_OK;
}
}
}
int
Cmd_DBMGetKey(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
int index;
datum key, data;
char *ptr;
#pragma unused (clientData)
if (argc != 3 && argc != 4)
{
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" DBName key ?varName?\"", (char *) NULL);
return TCL_ERROR;
}
for (index = 0 ; index < MAX_DBMS ; ++index)
{
if (strcmp(_dbms_[index].name, argv[1]) == SAMESTR)
break;
}
if (index >= MAX_DBMS)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" DB \"",
argv[1], "\" not found", (char *) NULL);
return TCL_ERROR;
}
else
{
key.dptr = argv[2];
key.dsize = strlen(argv[2]);
data = dbm_fetch(_dbms_[index].dbm, key);
if (data.dptr == NULL)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" key \"", argv[2],
"\" not found", (char *) NULL);
return TCL_ERROR;
}
else
{
ptr = malloc(data.dsize + 2);
if (ptr != NULL)
{
memcpy(ptr, data.dptr, data.dsize);
ptr[data.dsize] = '\0';
if (argc == 4)
Tcl_SetVar(interp, argv[3], ptr, 0);
else
Tcl_AppendResult(interp, ptr, (char *) NULL);
free(ptr);
return TCL_OK;
}
else
{
Tcl_AppendResult(interp, "\"", argv[0], "data too large to return", (char *) NULL);
return TCL_ERROR;
}
}
}
}
int
Cmd_DBMDelete(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
int index;
datum key;
#pragma unused (clientData)
if (argc != 3)
{
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" DBName key\"", (char *) NULL);
return TCL_ERROR;
}
for (index = 0 ; index < MAX_DBMS ; ++index)
{
if (strcmp(_dbms_[index].name, argv[1]) == SAMESTR)
break;
}
if (index >= MAX_DBMS)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" DB \"",
argv[1], "\" not found", (char *) NULL);
return TCL_ERROR;
}
else
{
key.dptr = argv[2];
key.dsize = strlen(argv[2]);
if (dbm_delete(_dbms_[index].dbm, key) < 0)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" key \"", argv[2],
"\" not found", (char *) NULL);
return TCL_ERROR;
}
else
{
return TCL_OK;
}
}
}
int
Cmd_DBMFirst(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
int index;
datum data;
char *ptr;
#pragma unused (clientData)
if (argc != 2 && argc != 3)
{
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" DBName ?varName?\"", (char *) NULL);
return TCL_ERROR;
}
for (index = 0 ; index < MAX_DBMS ; ++index)
{
if (strcmp(_dbms_[index].name, argv[1]) == SAMESTR)
break;
}
if (index >= MAX_DBMS)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" DB \"",
argv[1], "\" not found", (char *) NULL);
return TCL_ERROR;
}
else
{
data = dbm_firstkey(_dbms_[index].dbm);
if (data.dptr == NULL)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" database has no keys", (char *) NULL);
return TCL_ERROR;
}
else
{
ptr = malloc(data.dsize + 2);
if (ptr != NULL)
{
memcpy(ptr, data.dptr, data.dsize);
ptr[data.dsize] = '\0';
if (argc == 3)
Tcl_SetVar(interp, argv[2], ptr, 0);
else
Tcl_AppendResult(interp, ptr, (char *) NULL);
return TCL_OK;
}
else
{
Tcl_AppendResult(interp, "\"", argv[0], "key too large to return", (char *) NULL);
return TCL_ERROR;
}
}
}
}
int
Cmd_DBMNext(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
int index;
datum data;
char *ptr;
#pragma unused (clientData)
if (argc != 2 && argc != 3)
{
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" DBName ?varName?\"", (char *) NULL);
return TCL_ERROR;
}
for (index = 0 ; index < MAX_DBMS ; ++index)
{
if (strcmp(_dbms_[index].name, argv[1]) == SAMESTR)
break;
}
if (index >= MAX_DBMS)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" DB \"",
argv[1], "\" not found", (char *) NULL);
return TCL_ERROR;
}
else
{
data = dbm_nextkey(_dbms_[index].dbm);
if (data.dptr == NULL)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" no more keys", (char *) NULL);
return TCL_ERROR;
}
else
{
ptr = malloc(data.dsize + 2);
if (ptr != NULL)
{
memcpy(ptr, data.dptr, data.dsize);
ptr[data.dsize] = '\0';
if (argc == 3)
Tcl_SetVar(interp, argv[2], ptr, 0);
else
Tcl_AppendResult(interp, ptr, (char *) NULL);
return TCL_OK;
}
else
{
Tcl_AppendResult(interp, "\"", argv[0], "key too large to return", (char *) NULL);
return TCL_ERROR;
}
}
}
}
int
Cmd_DBMClose(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
int index;
#pragma unused (clientData)
if (argc != 2)
{
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" DBName\"", (char *) NULL);
return TCL_ERROR;
}
for (index = 0 ; index < MAX_DBMS ; ++index)
{
if (strcmp(_dbms_[index].name, argv[1]) == SAMESTR)
break;
}
if (index >= MAX_DBMS)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" DB \"",
argv[1], "\" not found", (char *) NULL);
return TCL_ERROR;
}
else
{
dbm_close(_dbms_[index].dbm);
_dbms_[index].dbm = (DBM *)0;
strcpy(_dbms_[index].name, "--CLOSED--");
return TCL_OK;
}
}
Tcl_InitDBM(interp)
Tcl_Interp *interp;
{
Tcl_CreateCommand(interp, "dbm_open", Cmd_DBMOpen,
(ClientData)NULL, (void (*)())NULL);
Tcl_CreateCommand(interp, "dbm_close", Cmd_DBMClose,
(ClientData)NULL, (void (*)())NULL);
Tcl_CreateCommand(interp, "dbm_insert", Cmd_DBMInsert,
(ClientData)NULL, (void (*)())NULL);
Tcl_CreateCommand(interp, "dbm_getkey", Cmd_DBMGetKey,
(ClientData)NULL, (void (*)())NULL);
Tcl_CreateCommand(interp, "dbm_delete", Cmd_DBMDelete,
(ClientData)NULL, (void (*)())NULL);
Tcl_CreateCommand(interp, "dbm_first", Cmd_DBMFirst,
(ClientData)NULL, (void (*)())NULL);
Tcl_CreateCommand(interp, "dbm_next", Cmd_DBMNext,
(ClientData)NULL, (void (*)())NULL);
}